package com.android.myfilm.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.android.myfilm.tools.Utils;
import com.facebook.android.R;

public class MyFilm extends Activity {
    /** Called when the activity is first created. */
    
	private static final String TAG = "MyFilm";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		
//		checkNetwork();
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
//		checkNetwork();
		
		EditText editText = ((EditText) findViewById(R.id.searchMovieEditText));
		editText.setText("");
		
//		IT DOESN'T WORK :-(
//		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//		imm.showSoftInput(editText, 0);
	}
	
	public void searchMovie(View button) {
		EditText editText = ((EditText) findViewById(R.id.searchMovieEditText));
		//Hide soft keyboard
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
		
		if (checkNetwork()) {
			final String searchMovieText = editText.getText().toString();
			
			if (searchMovieText == null || "".equals(searchMovieText)) {
				Toast.makeText(this.getApplicationContext(), "Please, enter a movie title.", Toast.LENGTH_SHORT).show();
			}
			else {
				Intent mIntent = new Intent(getApplicationContext(), MoviesList.class);
				mIntent.putExtra("searchMovieText", searchMovieText);
				startActivity(mIntent);
			}
		}
	}
	
//	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if (data == null) {
			setContentView(R.layout.layout);
			return;
		}
		
		Intent mIntent = new Intent(getApplicationContext(), MovieForm.class); 
		mIntent.putExtras(data.getExtras());
		startActivity(mIntent);
	}
	
	public void exit(View button) {
		finish();
	}
	
	private void showToast(String message){
		Toast.makeText(this.getApplicationContext(), message, Toast.LENGTH_LONG).show();
	}
	
	private boolean checkNetwork() {
		if (!Utils.isOnline(this)) {
//			showToast("Network is not available. Please check your settings and retry.");
			showAlertDialog("Network unavailable.");
			return false;
		}
		return true;
	}
	
	private void showAlertDialog(String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(message)
		       .setCancelable(false)
		       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   return;
//		        	   MyFilm.this.finish();
		           }
		       });
		builder.create().show();
//		AlertDialog alert = builder.create();
//		alert.show();
	}
}